﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKGeometryFuncs;

namespace YJKRevitStructModel.Data
{
	public class WallDataPack : ElementDataPack
	{
		public Curve LocatinCrv
		{
			get
			{
				return this.m_LocatinCrv;
			}
			set
			{
				this.m_LocatinCrv = value;
			}
		}

		public WallDataPack(Autodesk.Revit.DB.Document Doc, ElementId LinkInstanceId, ElementId elementId, Transform LinkDocTransForm) : base(Doc, LinkInstanceId, elementId, LinkDocTransForm, null)
		{
			ElementId typeId = this.m_doc.GetElement(this.m_elementId).GetTypeId();
			WallType wallType = this.m_doc.GetElement(typeId) as WallType;
			base.StrTypeName = wallType.Name;
		}

		public void GetWallCrv()
		{
			Curve curve = ((this.m_doc.GetElement(this.m_elementId) as Wall).Location as LocationCurve).Curve.CreateTransformed(this.m_LinkDocTransForm);
			if (curve is Arc)
			{
				this.m_LocatinCrv = curve;
			}
			List<Curve> list = null;
			base.GetZFaceLoop(out list);
			XYZ xyz = (curve as Line).Direction.Normalize();
			List<XYZ> list2 = new List<XYZ>();
			foreach (Curve curve2 in list)
			{
				if (Geometry.IsParallel((curve2 as Line).Direction.Normalize(), xyz))
				{
					list2.Add(curve2.GetEndPoint(0));
					list2.Add(curve2.GetEndPoint(1));
				}
			}
			XYZ xyz2 = curve.GetEndPoint(0);
			XYZ xyz3 = curve.GetEndPoint(1);
			xyz2 += xyz * -2.0;
			xyz3 += xyz * 2.0;
			curve = Line.CreateBound(xyz2, xyz3);
			double val = double.MaxValue;
			double val2 = double.MaxValue;
			XYZ xyz4 = XYZ.Zero;
			XYZ xyz5 = XYZ.Zero;
			foreach (XYZ xyz6 in list2)
			{
				XYZ xyzpoint = curve.Project(xyz6).XYZPoint;
				double num = xyz2.DistanceTo(xyz6);
				if (Geometry.LessThan(num, val))
				{
					val = num;
					xyz4 = xyz6;
				}
				num = xyz3.DistanceTo(xyz6);
				if (Geometry.LessThan(num, val2))
				{
					val2 = num;
					xyz5 = xyz6;
				}
			}
			XYZ xyzpoint2 = curve.Project(xyz4).XYZPoint;
			XYZ xyzpoint3 = curve.Project(xyz5).XYZPoint;
			this.m_LocatinCrv = Line.CreateBound(xyzpoint2, xyzpoint3);
		}

		public new bool GetZFaceLoop(out List<Curve> lstCrv)
		{
			lstCrv = new List<Curve>();
			Wall wall = this.m_doc.GetElement(this.m_elementId) as Wall;
			WallType wallType = wall.WallType;
			XYZ xyz = wall.Orientation.Normalize();
			if (this.m_LinkInstanceId == ElementId.InvalidElementId)
			{
				CompoundStructure compoundStructure = wallType.GetCompoundStructure();
				this.GetWallCrv();
				double num = 0.0;
				int num2 = 0;
				for (int i = compoundStructure.GetFirstCoreLayerIndex(); i <= compoundStructure.GetLastCoreLayerIndex(); i++)
				{
					CompoundStructureLayer compoundStructureLayer = compoundStructure.GetLayers()[i];
					if ((int)compoundStructureLayer.Function == 1 && Geometry.LessThan(num, compoundStructureLayer.Width))
					{
						num = compoundStructureLayer.Width;
						num2 = i;
					}
				}
				double num3 = 0.0;
				for (int j = 0; j < num2; j++)
				{
					num3 += compoundStructure.GetLayers()[j].Width;
				}
				Curve curve2;
				Curve curve3;
				if (this.m_LocatinCrv is Line)
				{
					Transform transform = Transform.CreateTranslation(xyz * (wall.Width / 2.0));
					Curve curve = this.m_LocatinCrv.CreateTransformed(transform);
					transform = Transform.CreateTranslation(xyz * -num3);
					curve2 = curve.CreateTransformed(transform);
					transform = Transform.CreateTranslation(xyz * -(num3 + num));
					curve3 = curve.CreateTransformed(transform);
				}
				else
				{
					Arc arc = this.m_LocatinCrv as Arc;
					Curve curve4 = Arc.Create(arc.Center, arc.Radius + wall.Width / 2.0 - num3, 0.0, Math.PI*2, arc.XDirection, arc.YDirection);
					Arc arc2 = Arc.Create(arc.Center, arc.Radius + wall.Width / 2.0 - num3 - num, 0.0, Math.PI*2, arc.XDirection, arc.YDirection);
					XYZ endPoint = arc.GetEndPoint(0);
					XYZ endPoint2 = arc.GetEndPoint(1);
					XYZ xyz2 = arc.Evaluate(0.5, true);
					XYZ xyzpoint = curve4.Project(endPoint).XYZPoint;
					XYZ xyzpoint2 = curve4.Project(endPoint2).XYZPoint;
					XYZ xyzpoint3 = curve4.Project(xyz2).XYZPoint;
					XYZ xyzpoint4 = arc2.Project(endPoint).XYZPoint;
					XYZ xyzpoint5 = arc2.Project(endPoint2).XYZPoint;
					XYZ xyzpoint6 = arc2.Project(xyz2).XYZPoint;
					curve2 = Arc.Create(xyzpoint, xyzpoint2, xyzpoint3);
					curve3 = Arc.Create(xyzpoint4, xyzpoint5, xyzpoint6);
				}
				lstCrv.Add(curve2);
				lstCrv.Add(Line.CreateBound(curve2.GetEndPoint(1), curve3.GetEndPoint(1)));
				lstCrv.Add(curve3.CreateReversed());
				lstCrv.Add(Line.CreateBound(curve3.GetEndPoint(0), curve2.GetEndPoint(0)));
				using (List<Curve>.Enumerator enumerator = lstCrv.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Curve curve5 = enumerator.Current;
						base.ArCrvEdage.Append(curve5);
					}
					return true;
				}
			}
			return base.GetZFaceLoop(out lstCrv);
		}

		private Curve m_LocatinCrv;
	}
}
